package cn.bocweb.spinning.view.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.TestRecordBean;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.RotateTextView;

/**
 * Created by chenchangxing on 15/12/24.
 */
public class MyTestNoteAdapter extends RecyclerView.Adapter {

    OnItemClickListener onItemClickListener;
    List<TestRecordBean.DataEntity> list;



    public interface OnItemClickListener {
        public void onClick(View view, int position);
    }

    public void setOnItemClickLitener(OnItemClickListener onItemClickLitener) {
        this.onItemClickListener = onItemClickLitener;
    }

    public MyTestNoteAdapter(List<TestRecordBean.DataEntity> list) {
        super();
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_test_note, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ViewHolder myHolder = (ViewHolder) holder;
        myHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = myHolder.getLayoutPosition();
                onItemClickListener.onClick(myHolder.itemView, position);
            }
        });
        int timeSpend = Integer.parseInt(list.get(position).getSpend_time());
        myHolder.examNotePoint.setText(list.get(position).getScore());
        String timeSpendStr = (timeSpend / 60) + "分" + (timeSpend % 60) + "秒";
        myHolder.examNoteSpendTime.setText(timeSpendStr);
        String timeStart = DeviceUtil.getDateTime(DeviceUtil.getDateFromMillis(Long.parseLong(list.get(position).getStart_time())));
        myHolder.examNoteStartTime.setText(timeStart);
        myHolder.examNoteLevel.setText(list.get(position).getSubtitle());

    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_my_test_note.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.exam_note_point)
        TextView examNotePoint;
        @Bind(R.id.exam_note_spend_time)
        TextView examNoteSpendTime;
        @Bind(R.id.exam_note_start_time)
        TextView examNoteStartTime;
        @Bind(R.id.exam_note_level)
        RotateTextView examNoteLevel;


        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
